import java.awt.EventQueue;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;


public class viewEdits {

	public JFrame frmEdits;
	private JTextField textField;
	private JScrollPane scrollPane;
	private JList list;
	DefaultListModel lm;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					viewEdits window = new viewEdits();
					window.frmEdits.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public viewEdits() {
		initialize();
		Database db = new Database();
		ArrayList<User> edits = db.getEdits();
		populate(edits);
	}
	
	public void populate(ArrayList<User> u){
		lm.clear();
		Database db = new Database();
		for(int i = 0; i < u.size(); i++){
			Person p = db.getPerson(u.get(i).personId);
			String text = u.get(i).username + " - " + u.get(i).edits + " - " + p.fname + " " + p.lname;
			lm.addElement(text);
		}
	}
	
	public void searching(){
		ArrayList<User> list = new ArrayList<User>();
		String str = textField.getText().toString();
		Database db = new Database();
		
		list = db.lookupEdits(str);
		
		populate(list);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmEdits = new JFrame();
		frmEdits.setTitle("Edits");
		frmEdits.setBounds(100, 100, 450, 300);
		frmEdits.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmEdits.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frmEdits.getContentPane().setLayout(null);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				frmEdits.setVisible(false);
				
			}
		});
		btnCancel.setBounds(335, 11, 89, 23);
		frmEdits.getContentPane().add(btnCancel);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 47, 414, 204);
		frmEdits.getContentPane().add(scrollPane);
		
		lm = new DefaultListModel();
		
		list = new JList(lm);
		scrollPane.setViewportView(list);
		
		textField = new JTextField();
		textField.setBounds(10, 12, 86, 20);
		frmEdits.getContentPane().add(textField);
		textField.setColumns(10);
		
		JButton btnFilter = new JButton("Filter User");
		btnFilter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				searching();
			}
		});
		btnFilter.setBounds(105, 11, 114, 23);
		frmEdits.getContentPane().add(btnFilter);
	}
}
